package ochart.renders
{
	import flash.events.EventDispatcher;
	import flash.filters.BlurFilter;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	
	import ochart.core.ChartObject;
	import ochart.events.RenderEvent;
	
	public class Renderer extends EventDispatcher implements IRenderer
	{
		public var _target:ChartObject = null;
		
		public function get target():ChartObject
		{
			return _target;
		}
		
		public function set target(value:ChartObject):void
		{
			_target = value;
		}
		
		public function get type():String
		{
			return null;
		}
		
		public function Renderer()
		{
		}
		
		public function render():Boolean
		{
			if(!target)
			{
				return false;
			}
			
			target.clear();
			
			renderBackground();
			renderBlur();
			renderBorder();
			renderGlow();
			renderShadow();
			
			return true;
		}
		
		public function renderBackground():void
		{
			if(!isNaN(target.backgroundColor))
			{
				target.graphics.beginFill(target.backgroundColor, target.backgroundAlpha);
				if(!target.backgroundCornerRadius || target.backgroundCornerRadius == 0)
				{
					target.graphics.drawRect(0, 0, target.width, target.height);
				}
				else if(target.backgroundCornerRadius is Number)
				{
					var ellipse:Number = Number(target.backgroundCornerRadius);
					target.graphics.drawRoundRect(0, 0, 
						target.width, target.height, 
						ellipse, ellipse);
				}
				else
				{
					var tl:Number = (target.backgroundCornerRadius.tl is Number) ? target.backgroundCornerRadius.tl : 0;
					var tr:Number = (target.backgroundCornerRadius.tr is Number) ? target.backgroundCornerRadius.tr : 0;
					var bl:Number = (target.backgroundCornerRadius.bl is Number) ? target.backgroundCornerRadius.bl : 0;
					var br:Number = (target.backgroundCornerRadius.br is Number) ? target.backgroundCornerRadius.br : 0;
					target.graphics.drawRoundRectComplex(0, 0, 
						target.width, target.height, 
						tl, tr, bl, br);
				}
				target.graphics.endFill();
			}
		}
		
		public function renderBorder():void
		{
			if(!isNaN(target.borderColor) && !isNaN(target.borderThickness))
			{
				var glowFilter:GlowFilter = new GlowFilter();
				glowFilter.alpha = target.borderAlpha;
				glowFilter.color = target.borderColor;
				glowFilter.blurX = target.borderThickness;
				glowFilter.blurY = target.borderThickness;
				glowFilter.strength = 20;
				glowFilter.inner = true;
				var filters:Array = target.filters;
				filters.push(glowFilter);
				target.filters = filters;
			}
		}
		
		public function renderBlur():void
		{
			if(target.blurX > 0 || target.blurY > 0)
			{
				var blurFilter:BlurFilter = new BlurFilter();
				blurFilter.blurX = target.blurX;
				blurFilter.blurY = target.blurY;
				blurFilter.quality = target.blurQuality;
				var filters:Array = target.filters;
				filters.push(blurFilter);
				target.filters = filters;
			}
		}
		
		public function renderGlow():void
		{
			if(!isNaN(target.glowColor))
			{
				var glowFilter:GlowFilter = new GlowFilter();
				glowFilter.alpha = target.glowAlpha;
				glowFilter.blurX = target.glowX;
				glowFilter.blurY = target.glowY;
				glowFilter.color = target.glowColor;
				glowFilter.knockout = target.glowKnockout;
				glowFilter.strength = target.glowStrength;
				glowFilter.inner = target.innerGlow;
				glowFilter.quality = target.glowQuality;
				var filters:Array = target.filters;
				filters.push(glowFilter);
				target.filters = filters;
			}
		}
		
		public function renderShadow():void
		{
			if(!isNaN(target.shadowColor))
			{
				var dropShadowFilter:DropShadowFilter = new DropShadowFilter();
				dropShadowFilter.alpha = target.shadowAlpha;
				dropShadowFilter.angle = target.shadowAngle;
				dropShadowFilter.blurX = target.shadowX;
				dropShadowFilter.blurY = target.shadowY;
				dropShadowFilter.color = target.shadowColor;
				dropShadowFilter.distance = target.shadowDistance;
				dropShadowFilter.inner = target.innerShadow;
				dropShadowFilter.knockout = target.shadowKnockout;
				dropShadowFilter.quality = target.shadowQuality;
				dropShadowFilter.strength = target.shadowStrength;
				var filters:Array = target.filters;
				filters.push(dropShadowFilter);
				target.filters = filters;
			}
		}
	}
}